package com.gandong8.hbase.web.controller;

import com.gandong8.hbase.tool.service.HbaseServiceInter;
import com.gandong8.hbase.web.domain.OrderDO;
import com.gandong8.hbase.web.domain.Result;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import java.util.*;

@Slf4j
@Controller
@RequestMapping(value = "/table",produces = "application/json;charset=UTF-8")
public class TableController   {
    @Autowired
    HbaseServiceInter hbaseServiceInter;

    @RequestMapping(value = { "", "/" })
    @ResponseBody
    public ModelAndView table() {
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.addObject("pageTitle", "表管理");
        modelAndView.setViewName("hbase/table");
        return modelAndView;
    }

    @RequestMapping(value = "/ajax/list")
    @ResponseBody
    public Result index() {
        return Result.success(hbaseServiceInter.listTableInfo());
    }

    @RequestMapping(value = "/ajax/update")
    @ResponseBody
    public Result update(String name, Integer enable) {
        if(StringUtils.isBlank(name) || enable == null) {
            return Result.failed("参数不能为空");
        }
        boolean result = enable == 1 ? hbaseServiceInter.enableTable(name):hbaseServiceInter.disableTable(name);
        return Result.run(result);
    }

    @RequestMapping(value = "/ajax/add", method = RequestMethod.POST)
    @ResponseBody
    public Result update(@RequestParam(value = "name") String name, @RequestParam(value = "families") String families) {
        if(StringUtils.isBlank(name) || StringUtils.isBlank(families)) {
            return Result.failed("参数不能为空");
        }
        Set<String> set = new HashSet<>(Arrays.asList(families.split(",")));
        hbaseServiceInter.createTable(name, set);
        return Result.success("ok");
    }

    @RequestMapping(value = "/ajax/del")
    @ResponseBody
    public Result del(String name) {
        if(StringUtils.isBlank(name)) {
            return Result.failed();
        }
        hbaseServiceInter.deleteTable(name);
        return Result.success();
    }

    @RequestMapping(value = "/ajax/family")
    @ResponseBody
    public Result family(String tableName) {
        return Result.success(hbaseServiceInter.listFamily(tableName));
    }

    @RequestMapping(value = "/test")
    @ResponseBody
    public Result test(String tableName) {
        OrderDO orderDO = new OrderDO();
        orderDO.setCreateTime(new Date());
        return Result.success(orderDO);
    }


}
